import ITool from '../ITool';
import Prefix from '../../constant/Prefix';

/**
 * 电压等级控制工具箱
 * @implements {ITool}
 */
class VoltageControlTool extends ITool {
  constructor(_top, _impl) {
    super(_top, _impl);
    this.name = 'VoltageControlTool';
    this.desc = '电压等级控制工具箱';
    //10kV、35kV、110kV、220kV、500kV，500kV及以上
    this.layerIdPrefixes = [Prefix.ActualGrid, Prefix.SimpleGrid];
    this.layerType = null;
    this.oldSelectedLayers = null;
  }

  onAdd() {
    super.onAdd();
  }

  onRemove() {
    super.onRemove();
  }

  /**
   * 设置电压等级过滤条件
   * @param {Object} options 过滤条件参数
   * @param {Array} selected 默认选中
   */
  setVoltageOption(options, selected) {
    let layerType = [];
    let layerAndFilterMap = {};
    Object.keys(options).forEach((key) => {
      let opt = options[key];
      if (opt[0] === 'range') {
        let range = opt[1];
        let filter = ['all'];
        if (range[0]) {
          filter.push(['>=', 'dydj', range[0]]);
        }
        if (range[1]) {
          filter.push(['<=', 'dydj', range[1]]);
        }
        layerAndFilterMap[key] = ['none', filter];
      } else if (opt[0] === 'enum') {
        let volts = opt[1];
        let filter = ['!in', 'dydj', ...volts];
        layerAndFilterMap[key] = filter;
      }
      layerType.push(key);
    });
    this.layerAndFilterMap = layerAndFilterMap;
    this.layerType = layerType;
    // 默认全选
    this.oldSelectedLayers = layerType;
    // 选中默认项
    if (selected) {
      this.show(selected);
    }
  }

  /**
   * 变换图层
   * @param {Array} layers 图层
   */
  show(layers) {
    let newSelectedLayers = layers;
    let statusChangedLayer = this.getStatusChangedLayer(newSelectedLayers, this.oldSelectedLayers);
    this.oldSelectedLayers = newSelectedLayers;
    statusChangedLayer.forEach((type) => {
      let visibility = newSelectedLayers.indexOf(type) > -1;
      this.changeLayerStatus(type, visibility);
    });
  }

  /**
   * 获取状态改变的图层
   * @param {Array} newSelectedLayers 新选中图层
   * @param {Array} oldSelectedLayers 旧选中图层
   * @ignore
   */
  getStatusChangedLayer(newSelectedLayers, oldSelectedLayers) {
    return this.layerType.filter((type) => {
      //找出需要改变状态的layer
      return newSelectedLayers.indexOf(type) > -1 !== oldSelectedLayers.indexOf(type) > -1;
    });
  }

  /**
   * 修改图层显示状态
   * @param {*} type 修改类型
   * @param {*} visibility 是否显示
   * @ignore
   */
  changeLayerStatus(type, visibility) {
    let layers = this.getControlLayersByPrefixes();
    let map = this.getMap();
    layers.forEach((layerId) => {
      let originalFilterArray = map.getFilter(layerId);
      let newFilter = this.layerAndFilterMap[type];
      let filter = this.getFilterByVisibility(originalFilterArray, newFilter, visibility);
      if(map.getLayer(layerId)) {
        map.setFilter(layerId, filter);
      }
    });
  }

  /**
   * 修改图层过滤器控制显示设备
   * @param {*} originalFilterArray 原图层过滤器
   * @param {*} newFilter 新过滤器
   * @param {*} visibility 是否可见
   * @ignore
   */
  getFilterByVisibility(originalFilterArray, newFilter, visibility) {
    for (let i = originalFilterArray.length - 1; i >= 0; i--) {
      if (originalFilterArray[i].toString() === newFilter.toString()) {
        originalFilterArray.splice(i, 1);
        break;
      }
    }
    if (!visibility) {
      originalFilterArray.push(newFilter);
    }
    return originalFilterArray;
  }

  /**
   * 根据前缀获取控制图层
   * @ignore
   */
  getControlLayersByPrefixes() {
    let layers = [];
    let totalLayers = Object.keys(this.getMap().style._layers);
    totalLayers.forEach((layerId) => {
      if (this.hasSamePrefix(layerId)) {
        layers.push(layerId);
      }
    });
    return layers;
  }

  /**
   * 图层Id是否含有指定前缀
   * @param {String} layerId 图层Id
   * @ignore
   */
  hasSamePrefix(layerId) {
    return this.layerIdPrefixes.some((prefix) => {
      return layerId.indexOf(prefix) === 0;
    });
  }
}

export default VoltageControlTool;
